﻿Namespace EIBData.DPT
    Public MustInherit Class EIBDataDPT_2_Base
        Inherits EIBDataDPT_Base

        Protected _control As Boolean = Nothing
        Protected _valueBase As Boolean = Nothing


        Sub New(data As Byte())
            If Not CanEncode(data) Then Throw New EIB.Exceptions.EIBDataDecodeException("")

            MyBase._data = data
            _control = (data(0) And &H2) >> 1
            _valueBase = (data(0) And &H1)
        End Sub

        Sub New(control As Boolean, value As Boolean)
            _control = control
            _valueBase = value
            MyBase._data = New Byte() {(If(_control, 1, 0) << 1) + If(_valueBase, 1, 0)}
        End Sub


        Public Overrides Function CanEncode(data() As Byte) As Boolean
            Return data IsNot Nothing AndAlso data.Length = DPT_OctetLength
        End Function

        Public Overrides Function ToString() As String
            If _control Then
                Return String.Format("Control. Function value {0}", If(_valueBase, 1, 0))
            Else
                Return "No control"
            End If
        End Function

        Public Overrides Function ToObject() As Object()
            Return New Object() {_control, _valueBase}
        End Function

        Public Overrides ReadOnly Property ObjectType As System.Type()
            Get
                Return New Type() {GetType(Boolean), GetType(Boolean)}
            End Get
        End Property

        Public Shared Shadows ReadOnly Property DPT_BaseName As String
            Get
                Return "B2"
            End Get
        End Property

        Public Shared Shadows ReadOnly Property DPT_OctetLength As Byte
            Get
                Return 1
            End Get
        End Property

        Public Shared Shadows ReadOnly Property DPT_Name As String
            Get
                Return "DPT_2"
            End Get
        End Property

        Public Shared Shadows ReadOnly Property DPT_Number As String
            Get
                Return "2.xxx"
            End Get
        End Property


        Public ReadOnly Property Control As Boolean
            Get
                Return _control
            End Get
        End Property

        Public ReadOnly Property Value As Boolean
            Get
                Return _valueBase
            End Get
        End Property
    End Class
End Namespace